package com.wanbangee.marksystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wanbangee.marksystem.entities.MExamMarker;
import com.wanbangee.marksystem.service.QualityCheckService;
import com.wanbangee.marksystem.service.score.SettingScoreService;

@RestController
@RequestMapping("quality")
public class QualityCheckController {
	
	@Autowired
	private SettingScoreService mExamMarkerInfoService;
	 
	@Autowired
	private QualityCheckService qualityCheckService;
	
	/**
	 * 用于图表显示
	 * @param examId
	 * @param examSubId
	 * @param scoreType
	 * @param topicNo
	 * @return
	 */
	@RequestMapping("selectQualityCharInfo/{examId}/{examSubId}/{topicNo}")
	public Map<String,Object> selectQualityCharInfo( 
				@PathVariable("examId")Integer examId,
				@PathVariable("examSubId")Integer examSubId,
				@PathVariable("topicNo")Integer topicNo){
		
		HashMap<String,Object> result = new HashMap<String,Object>();
		List<MExamMarker> mExamMarkers =  mExamMarkerInfoService.selectQualityCharInfo(examId, examSubId, topicNo);
		
		if(mExamMarkers.size() > 0 ) {
			result.put("code", 1001);
			result.put("result", mExamMarkers);
		}else {
			result.put("code", 10013);
			result.put("result", null);
		}
		
		return result;
	}
	
	/**
	 * 查询整体的平均分、最高分、最低分
	 */
	@GetMapping("selectAllAvgMavMin/{examId}/{examSubId}/{topicNo}/{scoreType}")
	public Map<String,Object> selectAllAvgMaxMin(@PathVariable("examId")Integer examId,@PathVariable("examSubId")Integer examSubId,@PathVariable("topicNo")Integer topicNo,@PathVariable("scoreType")Integer scoreType) {
		HashMap<String,Object> result = new HashMap<String,Object>();
		Map scores = qualityCheckService.selectAllAvgMaxMin(examId, examSubId, topicNo, scoreType);
		result.put("code",1001);
		result.put("result", scores);
		return result;
	}

	/**
	 * 判断是否为学科负责人
	 * @param examId
	 * @param examSubId
	 * @param teaId
	 * @return
	 */
	@GetMapping("selectIsPrincipalTea/{examId}/{examSubId}/{teaId}")
	public Map<String,Object> selectIsPrincipalTea(@PathVariable("examId")Integer examId,@PathVariable("examSubId")Integer examSubId,@PathVariable("teaId")Integer teaId) {
		return mExamMarkerInfoService.selectIsPrincipalTea(examId, examSubId, teaId);
	}
	/**
	 * 查询老师的阅卷信息
	 * @param markerId
	 * @param order
	 * @param examId
	 * @param examSubId
	 * @param topicNo
	 * @return
	 */
	@GetMapping("selectExamTeaInfo/{markerId}/{order}/{examId}/{examSubId}/{topicNo}")
	public Map<String, Object> selectExamTeaInfo(@PathVariable("markerId")Integer markerId,
			@PathVariable("order")Integer order,
			@PathVariable("examId")Integer examId,
			@PathVariable("examSubId")Integer examSubId,
			@PathVariable("topicNo")Integer topicNo) {
		HashMap<String,Object> result = new HashMap<String,Object>();
		MExamMarker mExamMarker = mExamMarkerInfoService.selectExamTeaInfo(markerId);
		if (mExamMarker!=null) {
			mExamMarker.setScoreDetails(qualityCheckService.selectSScoreDetails(examId, examSubId, topicNo, markerId, order, mExamMarker.getScoreType()));
			result.put("code", "1001");
			result.put("result", mExamMarker);
		}else {
			result.put("code", "10013");
			result.put("result", null);
		}
		
		return result;
	}
	
	@GetMapping("/selectMExamMarkerList/{examId}/{examSubId}/{topicNo}")
	public Map<String, Object> selectMExamMarkerList(@PathVariable("examId")Integer examId,@PathVariable("examSubId")Integer examSubId,@PathVariable("topicNo")Integer topicNo) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		List<MExamMarker> mExamMarkers = mExamMarkerInfoService.selectMExamMarkerList(examId, examSubId, topicNo);
		if (mExamMarkers.size()>0) {
			result.put("code", "1001");
			result.put("result", mExamMarkers);
		}else {
			result.put("code", "10013");
			result.put("result", null);
		}
		return result;
	}
	
	@PutMapping("/updateCheckScore")
	public Map<String, Object> updateCheckScore(@RequestParam("examId")Integer examId,@RequestParam("examSubId")Integer examSubId,@RequestParam("scoreId")Integer scoreId,@RequestParam("repeatCause")String repeatCause) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		Integer updateNum = qualityCheckService.updateCheckScore(examId, examSubId, scoreId, repeatCause);
		if (updateNum==1) {
			result.put("code", "1001");
			result.put("result", updateNum);
		}else {
			result.put("code", "10013");
			result.put("result", 0);
		}
		return result;
	}
	
	
}
